package com.wangjie.secondary;

/**
 * @author jieshao
 * @date 2022/5/13 14:20
 *
 * 01. 一次编辑
 */
public class Test01 {
    public static void main(String[] args) {
        String first = "pales";
        String second = "pal";
        boolean res = oneEditAway(first, second);
        System.out.println(res);
    }

    public static boolean oneEditAway(String first, String second) {
        int len1 = first.length();
        int len2 = second.length();
        if (len1 - len2 == 1) {
            return oneInsert(second, first);
        } else if (len2 - len1 == 1) {
            return oneInsert(first, second);
        } else if (len1 == len2) {
            int count = 0;
            for (int i = 0; i < len1; i++) {
                if (first.charAt(i) != second.charAt(i)) {
                    count++;
                }
                if (count > 1) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    public static boolean oneInsert(String shorter, String longger) {
        int index1 = 0;
        int index2 = 0;
        while (index1 < shorter.length() && index2 < longger.length()) {
            if (shorter.charAt(index1) == longger.charAt(index2)) {
                index1++;
            }
            index2++;
            if (index2 - index1 > 1) {
                return false;
            }
        }
        return true;
    }
}